package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_template;
import cn.com.api.service.ApiU_templateService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "u_template/")
public class ApiU_templateController {
    @Autowired
    private ApiU_templateService apiU_templateService;
	
	private static Logger logger = Logger.getLogger(ApiU_templateController.class);
	
	/**
	 * 新增
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "insertU_template")
	public void insertU_template(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String template_id = request.getParameter("template_id");
		String tpl_name = request.getParameter("tpl_name");
		String tpl_thumb = request.getParameter("tpl_thumb");
		String bg_image = request.getParameter("bg_image");
		String bg_audio = request.getParameter("bg_audio");
		String type = request.getParameter("type");
		String tpl_desc = request.getParameter("tpl_desc");
		String tpl_level = request.getParameter("tpl_level");
		String create_time = request.getParameter("create_time");
		String create_by = request.getParameter("create_by");
		String modify_time = request.getParameter("modify_time");
		String modify_by = request.getParameter("modify_by");
		String status = request.getParameter("status");
		String controller_route = request.getParameter("controller_route");
		String theme = request.getParameter("theme");
		String parameters = request.getParameter("parameters");
		String ext_1 = request.getParameter("ext_1");
		String ext_2 = request.getParameter("ext_2");
		String ext_3 = request.getParameter("ext_3");
		String ext_4 = request.getParameter("ext_4");
		String ext_5 = request.getParameter("ext_5");
        
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(template_id!=null && !"".equals(template_id.trim())){
				ApiU_template u_template = new ApiU_template();
				
    		    //u_template.setTemplate_id(template_id);
    		    //u_template.setTpl_name(tpl_name);
    		    //u_template.setTpl_thumb(tpl_thumb);
    		    //u_template.setBg_image(bg_image);
    		    //u_template.setBg_audio(bg_audio);
    		    //u_template.setType(type);
    		    //u_template.setTpl_desc(tpl_desc);
    		    //u_template.setTpl_level(tpl_level);
    		    //u_template.setCreate_time(create_time);
    		    //u_template.setCreate_by(create_by);
    		    //u_template.setModify_time(modify_time);
    		    //u_template.setModify_by(modify_by);
    		    //u_template.setStatus(status);
    		    //u_template.setController_route(controller_route);
    		    //u_template.setTheme(theme);
    		    //u_template.setParameters(parameters);
    		    //u_template.setExt_1(ext_1);
    		    //u_template.setExt_2(ext_2);
    		    //u_template.setExt_3(ext_3);
    		    //u_template.setExt_4(ext_4);
    		    //u_template.setExt_5(ext_5);
				
				apiU_templateService.insertU_template(u_template);
				// return
                map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_templateById")
	public void findU_templateById(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    String id = request.getParameter("id");
		    ApiU_template u_template = apiU_templateService.findU_templateById(Integer.valueOf(id));
		
			// return
            map.put("datas", u_template);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
     /**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_templates")
	public void findU_templates(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    List<ApiU_template> u_templates = apiU_templateService.findU_templates();
		
			// return
            map.put("datas", u_templates);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
    
	/**
	 * 修改
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@Transactional
	@RequestMapping(value = "updateU_template")
	public void updateU_template(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String template_id = request.getParameter("template_id");
		String tpl_name = request.getParameter("tpl_name");
		String tpl_thumb = request.getParameter("tpl_thumb");
		String bg_image = request.getParameter("bg_image");
		String bg_audio = request.getParameter("bg_audio");
		String type = request.getParameter("type");
		String tpl_desc = request.getParameter("tpl_desc");
		String tpl_level = request.getParameter("tpl_level");
		String create_time = request.getParameter("create_time");
		String create_by = request.getParameter("create_by");
		String modify_time = request.getParameter("modify_time");
		String modify_by = request.getParameter("modify_by");
		String status = request.getParameter("status");
		String controller_route = request.getParameter("controller_route");
		String theme = request.getParameter("theme");
		String parameters = request.getParameter("parameters");
		String ext_1 = request.getParameter("ext_1");
		String ext_2 = request.getParameter("ext_2");
		String ext_3 = request.getParameter("ext_3");
		String ext_4 = request.getParameter("ext_4");
		String ext_5 = request.getParameter("ext_5");
        
        
		//String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{ 
			if(template_id!=null && !"".equals(template_id.trim())){
				ApiU_template u_template = new ApiU_template(); 
				//获取原数据
				u_template  = apiU_templateService.findU_templateById(Integer.valueOf(template_id));
				//修改
				//u_template.setIs_open(Integer.valueOf(is_open));
				//
				int ret_i = apiU_templateService.updateU_template(u_template);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			}else{
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		}
		catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
    
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "deleteU_template")
	public void deleteU_template(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
        String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
	
				Integer ret_I = apiU_templateService.deleteU_template(Integer.parseInt(id));
				if(ret_I >0 ){
    				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
    				map.put(Utils.MESSAGE, "删除成功！");
                }else{
				    map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				    map.put(Utils.MESSAGE, "检查失败！");
			    }
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
 
	
	   /**
		 * 查询数据集根据模块
		 * 
		 * @param request
		 * @param response
		 * @return
		 */
		@RequestMapping("findU_templates_type")
		public void findU_templates_type(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
			Map<String,Object> map = new HashMap<String,Object>();
			try{
				String type = request.getParameter("type");
				logger.info("==========[ApiU_templateController[u_template/findU_templates_type接口参数[type:"+type+"]]]");
				List<Map<String, Object>> u_templates = apiU_templateService.findU_templates_type(type);
	            map.put("datas", u_templates);
	            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
			}catch(Exception e){
				logger.error("==========[ApiU_templateController[u_template/findU_templates_type接口错误]]]",e);
	            e.printStackTrace();  
	    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
	    		map.put(Utils.MESSAGE, "系统异常！");
			}

			JsonUtil.printJson(response, JsonUtil.object2json(map));
	        
		}
	
}


